﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="wNewMessage.xaml.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Interaction logic for wNewMessage.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

// ReSharper disable InconsistentNaming
namespace Mar3ek.HouseLight
{
    using System.Windows;
    using LighthouseNET;

    /// <summary>
    /// Interaction logic for wNewMessage.xaml
    /// </summary>
    public partial class wNewMessage
    {
        private readonly int _projectId;
        private readonly LighthouseAPI _api;
        private readonly bool _editing;
        private readonly bool _comment;
        private readonly Message _message;
        private readonly int _parentId;

        /// <summary>
        /// Initializes a new instance of the <see cref="wNewMessage"/> class. 
        /// </summary>
        /// <param name="api">
        /// The api instance.
        /// </param>
        /// <param name="projectId">
        /// The project Id.
        /// </param>
        public wNewMessage(LighthouseAPI api, int projectId)
        {
            InitializeComponent();
            _projectId = projectId;
            _api = api;
            Result = false;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="wNewMessage"/> class.
        /// </summary>
        /// <param name="api">The API.</param>
        /// <param name="projectId">The project id.</param>
        /// <param name="messageId">The parent message id.</param>
        public wNewMessage(LighthouseAPI api, int projectId, int messageId)
        {
            InitializeComponent();
            _projectId = projectId;
            _api = api;
            _comment = true;
            _parentId = messageId;
            Title = "New comment";
            Result = false;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="wNewMessage"/> class. 
        /// </summary>
        /// <param name="api">
        /// The API instance
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        public wNewMessage(LighthouseAPI api, Message message)
        {
            InitializeComponent();
            _api = api;
            Title = "Editing message";
            _message = message;
            _editing = true;
            _projectId = message.ProjectId;
            txtTitle.Text = message.Title;
            txtBody.Text = message.Body;
            Result = false;
        }

        /// <summary>
        /// Gets a value indicating whether an update has been sent.
        /// </summary>
        /// <value>
        ///   <c>true</c> if sent; otherwise, <c>false</c>.
        /// </value>
        internal bool Result { get; private set; }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Handles the Click event of the btnSubmit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtTitle.Text) || string.IsNullOrEmpty(txtBody.Text))
            {
                return;
            }

            try
            {
                var message = new Message(txtTitle.Text, txtBody.Text);
                if (_editing)
                {
                    _api.UpdateMessage(_projectId, _message.Id, message);
                }
                else if (_comment)
                {
                    _api.CreateComment(_projectId, _parentId, message);
                }
                else
                {
                    _api.CreateMessage(_projectId, message);
                }

                Result = true;
                Close();
            }
            catch (LighthouseResponseException ex)
            {
                MessageBox.Show(
                    ex.Message,
                    "HouseLight",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            catch (LighthouseServerException ex)
            {
                MessageBox.Show(
                    string.Format("A communication error was encountered. Check your internet connection and try again.\nIf the problem persists, contact the plugin developer.\nError details follow:\n\n{0}", ex.Message),
                    "HouseLight",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }
    }
}

// ReSharper restore InconsistentNaming